//
//  DeWeightViewController.m
//  UEAssistant
//
//  Created by mac on 2017/9/8.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "DeWeightViewController.h"
#import "QuchongHeaderView.h"
#import "QuchongTableViewCell.h"
#import <IQKeyboardManager.h>
#import "DewResultViewController.h"

#define Screen_width           [UIScreen mainScreen].bounds.size.width
#define Screen_height          [UIScreen mainScreen].bounds.size.height

@interface DeWeightViewController ()<UITableViewDelegate,UITableViewDataSource,ShowMoreCellDelegate>{
    
    NSMutableArray          *_dataSource;
    NSInteger               _cellRow;
    NSInteger               _cellRow_MT;
    NSMutableArray          *_selectModel;
    NSMutableArray          *_selectModel_MT;
    NSArray                 *_ifFreeArr;
    NSArray                 *_deweightTitle;
    NSArray                 *_deweightSubTitle;
    NSArray                 *_ifdupallArr;
    NSArray                 *_ifMTArr;
    NSMutableArray          *_languageArray;
    NSMutableArray          *_switchOnArr;
    NSInteger               _ifdupall;
    NSInteger               _ifUseMT;
}

@property (nonatomic,strong) UITableView            *tableView;
@property (nonatomic,strong) UILabel                *NaviTitle;
@property (nonatomic,strong) UITextField            *numtextField;
@property (nonatomic,strong) UITextField            *numtextField_MT;
@property (nonatomic,strong) UIButton               *rightButon;
@property (nonatomic,strong) NSString               *sourceKey;


@end

@implementation DeWeightViewController


- (void)viewWillAppear:(BOOL)animated{
    
    [self.NaviTitle setHidden:NO];
    [self.rightButon setHidden:NO];
    [IQKeyboardManager sharedManager].enable = YES;

}

- (void)viewWillDisappear:(BOOL)animated{
    
    [self.NaviTitle setHidden:YES];
    [self.rightButon setHidden:YES];
    [IQKeyboardManager sharedManager].enable = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self initData];
}

- (void)initUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:self.NaviTitle];
    [self.navigationController.navigationBar addSubview:self.rightButon];
    _ifFreeArr = @[@"免费",@"免费",@"免费",@"收费",@"限时1折"];
    _deweightTitle = @[@"非译去重",@"文件去重",@"团队语料去重",@"共享语料去重",@"使用机器翻译"];
    _deweightSubTitle = @[@"锁定纯数字句段等明显不需要翻译的内容",@"锁定项目文件内或文件间完全相同的句子",@"自动翻译并锁定与团队记忆库数据相同的句段",@"自动翻译并锁定与共享记忆库数据相同的句段",@"用选定的MT引擎自动翻译"];
    _ifdupallArr = @[@"分析项目所有文件",@"只分析选定的文件"];
    _ifMTArr = @[@"所有未翻句段",@"限              字以内（含）的字段"];
    [self.view addSubview:self.tableView];

}


- (void)initData{
    
    _dataSource = [NSMutableArray array];
    _cellRow = 2;
    _cellRow_MT = 2;
    _selectModel = [NSMutableArray arrayWithObjects:@"1",@"0",nil];
    _selectModel_MT = [NSMutableArray arrayWithObjects:@"1",@"0",nil];
    _switchOnArr = [NSMutableArray arrayWithObjects:@"1",@"1",@"1",@"1",@"1",nil];
}

#pragma Evnet

- (void)onClickedNextBtn{

    [_numtextField resignFirstResponder];
    DewResultViewController *vc = [[DewResultViewController alloc]init];
    //传递去重需要的参数
    vc.docid = self.doc_id;
    vc.doc_namesuffix = self.doc_name_suffix;
    vc.switchList = [NSString stringWithFormat:@"%@%@%@%@%@",_switchOnArr[0],_switchOnArr[1],_switchOnArr[2],_switchOnArr[3],_switchOnArr[4]];
    vc.minLength = _numtextField.text == nil ? @"1" : _numtextField.text;
    vc.ifdupall = [NSString stringWithFormat:@"%@",_selectModel[0]];
    if ([_selectModel_MT[1] isEqualToString:@"1"] && [_switchOnArr[4] isEqualToString:@"1"]) {
        vc.mt = [NSString stringWithFormat:@"%@",_numtextField_MT.text];
    }else{
        vc.mt = @"0";//mt，0的代表不限制
    }
    TGLog(@"%@",vc.mt);
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 解析json数据
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:nil];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
    
}


#pragma mark -- <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 1) {
        return _cellRow;
    }else if (section == 4){
        return _cellRow_MT;
    }
    else{
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section != 5) {
        QuchongHeaderView *header = [[QuchongHeaderView alloc]initWithReuseIdentifier:@"header"];
        [header sectionNumber:section];
        header.delegate = self;
        header.titleName.text = _deweightTitle[section];
        header.remarkText.text = _deweightSubTitle[section];
        header.ifFree.text = _ifFreeArr[section];
        
        CGSize size = [[NSString stringWithFormat:@"%@",_deweightTitle[section]] sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]}];
        CGSize sizeNew = CGSizeMake(ceilf(size.width), ceilf(size.height));
        header.titleName.frame = CGRectMake(20, 13, sizeNew.width, 20);
        header.ifFree.frame = CGRectMake(header.titleName.frame.origin.x+header.titleName.frame.size.width+5, header.titleName.frame.origin.y+header.titleName.frame.size.height - 17, 25, 15);
        if ([_ifFreeArr[section] isEqualToString:@"限时1折"]) {
            header.ifFree.frame = CGRectMake(header.titleName.frame.origin.x+header.titleName.frame.size.width, header.titleName.frame.origin.y+header.titleName.frame.size.height - 17, 45, 15);
        }
        if ([_ifFreeArr[section] isEqualToString:@"免费"] || [_ifFreeArr[section] isEqualToString:@"VIP免费"]) {
            header.ifFree.backgroundColor = RGB(47, 155, 210, 1);
            if ([_ifFreeArr[section] isEqualToString:@"VIP免费"]) {
                header.ifFree.frame = CGRectMake(header.titleName.frame.origin.x+header.titleName.frame.size.width, header.titleName.frame.origin.y+header.titleName.frame.size.height - 17, 45, 15);
            }
        }else{
            header.ifFree.backgroundColor = RGB(250, 198, 82, 1);
        }
        
        if (_cellRow == 2 && section == 1) {
            [header.switchV setOn:YES];
        }else if (_cellRow == 0 && section == 1){
            [header.switchV setOn:NO];
        }else if (_cellRow_MT == 2 && section == 4){
            [header.switchV setOn:YES];
        }else if (_cellRow_MT == 0 && section == 4){
            [header.switchV setOn:NO];
        }
        else{
            [header.switchV setOn:YES];
        }
        return header;
    }
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, 130)];
    header.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, Screen_Width-30, 60)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = RGB(109, 109, 109, 1);
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.text = @"少于或等于              个字的句段不参与文件去重和语料去重";
    [header addSubview:label];

    _numtextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 22, 50, 20)];
    _numtextField.textAlignment = NSTextAlignmentCenter;
    _numtextField.backgroundColor = [UIColor clearColor];
    _numtextField.textColor = RGB(51, 51, 51, 1);
    _numtextField.font = [UIFont systemFontOfSize:16];
    _numtextField.borderStyle = UITextBorderStyleRoundedRect;
    _numtextField.keyboardType = UIKeyboardTypeNumberPad;
    _numtextField.text = @"1";
    [header addSubview:_numtextField];

    UILabel *subLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, label.frame.origin.y+label.frame.size.height, Screen_Width-30, 60)];
    subLabel.backgroundColor = [UIColor clearColor];
    subLabel.textColor = RGB(109, 109, 109, 1);
    subLabel.font = [UIFont systemFontOfSize:13];
    subLabel.numberOfLines = 0;
    subLabel.text = @"少于或等于指定字数的句段不参与文件去重、团队语料和共享语料去重，非译去重不受此限制。填写0表示所有句段均参与去重。";
    [header addSubview:subLabel];
    
    
    return header;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section == 4) {
        UIView *view = nil;
        return view;
    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, 15)];
    view.backgroundColor = RGB(247, 247, 247, 1);
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 5) {
        return 130;
    }
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QuchongTableViewCell *cell = [[QuchongTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 1) {
        cell.title.text = _ifdupallArr[indexPath.row];
        if ([_selectModel[indexPath.row] integerValue] == 1) {
            cell.selectImage.image = [UIImage imageNamed:@"选中_spli"];
        }else{
            cell.selectImage.image = [UIImage imageNamed:@"取消_spli"];
        }
    }else{
        if (indexPath.row == 1) {
            _numtextField_MT = [[UITextField alloc] initWithFrame:CGRectMake(70, 15, 50, 20)];
            _numtextField_MT.textAlignment = NSTextAlignmentCenter;
            _numtextField_MT.backgroundColor = [UIColor clearColor];
            _numtextField_MT.textColor = RGB(51, 51, 51, 1);
            _numtextField_MT.font = [UIFont systemFontOfSize:16];
            _numtextField_MT.borderStyle = UITextBorderStyleRoundedRect;
            _numtextField_MT.keyboardType = UIKeyboardTypeNumberPad;
            _numtextField_MT.text = @"1";
            [cell addSubview:_numtextField_MT];
        }
        cell.title.text = _ifMTArr[indexPath.row];
        if ([_selectModel_MT[indexPath.row] integerValue] == 1) {
            cell.selectImage.image = [UIImage imageNamed:@"选中_spli"];
        }else{
            cell.selectImage.image = [UIImage imageNamed:@"取消_spli"];
        }
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        [_selectModel enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj integerValue] == 1) {
                [_selectModel replaceObjectAtIndex:idx withObject:@"0"];
            }
        }];
        if ([_selectModel[indexPath.row] integerValue] == 0) {
            [_selectModel replaceObjectAtIndex:indexPath.row withObject:@"1"];
        }else{
            [_selectModel replaceObjectAtIndex:indexPath.row withObject:@"0"];
        }
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
    }else if (indexPath.section == 4){
        [_selectModel_MT enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj integerValue] == 1) {
                [_selectModel_MT replaceObjectAtIndex:idx withObject:@"0"];
            }
        }];
        if ([_selectModel_MT[indexPath.row] integerValue] == 0) {
            [_selectModel_MT replaceObjectAtIndex:indexPath.row withObject:@"1"];
        }else{
            [_selectModel_MT replaceObjectAtIndex:indexPath.row withObject:@"0"];
        }
        
        [UIView performWithoutAnimation:^{
            NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:4];
            [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];

        }];
    }

}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}


#pragma mark -- <ShowMoreCellDelegate>
- (void)switchValueYesOrNo:(BOOL)value sectionNum:(NSInteger)num{
    
    if ( num == 1) {
        if (!value) {
            _cellRow = 0;
            _ifdupall = 1;
            [_switchOnArr replaceObjectAtIndex:num withObject:@"0"];
        }else{
            [_switchOnArr replaceObjectAtIndex:num withObject:@"1"];
            _cellRow = 2;
        }
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    }else if (num == 4){
        if (!value) {
            _cellRow_MT = 0;
            _ifUseMT = 1;
            [_switchOnArr replaceObjectAtIndex:num withObject:@"0"];
        }else{
            [_switchOnArr replaceObjectAtIndex:num withObject:@"1"];
            _cellRow_MT = 2;
        }
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:4];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else{
        if (!value) {
            [_switchOnArr replaceObjectAtIndex:num withObject:@"0"];
        }else{
            [_switchOnArr replaceObjectAtIndex:num withObject:@"1"];
        }
    }
}


#pragma mark -- getter
- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_width,Screen_height-64) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = RGB(247, 247, 247, 1);
        _tableView.dataSource = self;
        _tableView.delegate = self;

    }
    return _tableView;
}

- (UILabel *)NaviTitle{
    
    if (!_NaviTitle) {
        _NaviTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
        _NaviTitle.center = CGPointMake(self.view.center.x, 20);
        _NaviTitle.backgroundColor = [UIColor clearColor];
        _NaviTitle.text = @"去重设置";
        _NaviTitle.textAlignment = NSTextAlignmentCenter;
        _NaviTitle.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _NaviTitle;
}

- (UIButton *)rightButon{
    
    if (!_rightButon) {
        _rightButon = [UIButton buttonWithType:UIButtonTypeSystem];
        _rightButon.backgroundColor = [UIColor clearColor];
        _rightButon.frame = CGRectMake(Screen_Width - 80, 5, 80, 32);
        [_rightButon setTitle:@"下一步" forState:UIControlStateNormal];
        [_rightButon setTitleColor:RGB(51, 51, 51, 1) forState:UIControlStateNormal];
        _rightButon.titleLabel.font = [UIFont systemFontOfSize:17];
        [_rightButon addTarget:self action:@selector(onClickedNextBtn) forControlEvents:UIControlEventTouchUpInside];
        _rightButon.layer.cornerRadius = 16;
        _rightButon.layer.masksToBounds = YES;
        
    }
    return _rightButon;
}

@end
